package su.mvc.action;

import com.googlecode.objectify.Key;
import com.opensymphony.xwork2.ModelDriven;
import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.*;
import su.mvc.model.MenuItem;
import su.mvc.service.gui.GuiService;

import java.util.logging.Logger;

@Results(value = {
        @Result(name = ActionSupport.SUCCESS, location = "page.jsp")
})
public class PageAction extends ActionSupport implements ModelDriven<MenuItem> {

    private static final Logger log = Logger.getLogger(PageAction.class.getName());


    private String id;
    private MenuItem model;
    private Key key;

    private GuiService guiService;
    
    public MenuItem getModel() {
        return model;  
    }

    @Actions(
            value = {
                    @Action(value = "/"),
                    @Action(value = "/page")
            }
    )
    public String execute() throws Exception{
        return show();
    }

    public String show() throws Exception {
        if (StringUtils.isEmpty(id) && key == null) {
            key= guiService.getMenuService().getTOP().getKey();
        }
        guiService.setObjectify(getObjectify());
        model = guiService.getContentByIdOrKey(key, id);
        return SUCCESS;
    }

    public Key getKey() {
        return key;
    }

    public void setKey(Key key) {
        this.key = key;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public GuiService getGuiService() {
        return guiService;
    }

    public void setGuiService(GuiService guiService) {
        this.guiService = guiService;
    }
}
